import {roads} from './role.road_repairer'

export = function (sourceId: string, targetIds: string[], roomName: string): ICreepConfig {
    return {
        //搬运
        source: (creep: Creep) => {
            const room = Game.rooms[roomName];
            if (!room || creep.room.name != room.name)
                creep.moveTo(new RoomPosition(39, 10, roomName));
            else if (creep.room.name == room.name) {
                creep.moveTo(new RoomPosition(39, 10, roomName));
                if (creep.room.name == room.name) {
                    let res: Resource = creep.room.find(FIND_DROPPED_RESOURCES)[0];
                    if (creep.pickup(res) == ERR_NOT_IN_RANGE) creep.moveTo(res);
                    let container: StructureContainer = Game.getObjectById("5eb4af5d3466e0893c12f36f");
                    if (container.store.getUsedCapacity() > 0 && creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(container);
                }
            }

        },
        target: (creep: Creep) => {
            for (let i = 0; i < targetIds.length; i++) {
                let targetId: string = targetIds[i];
                let target: any = Game.getObjectById<Structure>(targetId);
                if (target.store != null && target.store.getFreeCapacity(RESOURCE_ENERGY) > 0) {
                    _.filter(Object.values(creep.room.find(FIND_STRUCTURES)),
                        structure => structure.structureType == STRUCTURE_ROAD)
                        .forEach(road => creep.repair(road));
                    if (creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(target, {reusePath: 50});
                        return
                    }
                }
            }
        },
        // 状态切换条件，稍后会给出具体实现
        switch: (creep: Creep) => creep.updateState()
    }
}